<?php 
namespace app\common\model;
use think\Model;
class Common extends Model{
	protected function getSysManageAttr($value,$data){
		return "<a href='".url("edit",['id'=>$data['id']])."' isForm class='cocoModal'>编辑</a> | <a href='".url("delete",['id'=>$data['id']])."'>删除</a>";
	}
	public function save_data($data){
		$result = $this->validate(true)->allowField(true)->isUpdate(false)->data($data)->save();
		
		if($result!==false){
			$pk = $this->getPk();
			$id = $this->$pk;
			return $id;
		}
		return false;
	}
	public function delete_data($data){
		$ids = $data['item_ids'];
		return $this::destroy($ids);
	}
	public function update_data($data){
		$result = $this->validate(true)->allowField(true)->isUpdate(true)->save($data);
		if($result!==false){
			$pk = $this->getPk();
			$id = $data[$pk];
			return $id;
		}
		return false;
	}
	public function cache_data($data = []){
		$datas = [];
		$pk = $this->getPk();
		$list = $this->order($pk, 'desc')->select();
        foreach($list as $v){
            $datas[$v['id']] = $v;
        }
        cache($this->name, $datas);
	}
	public function get_data($data,$except = '',$field=''){
		$id = $data['item_id'];
		$pk = $this->getPk();
		if($except != ''){
			$this->field($except,true);
		}elseif ($except == '' && $field != '') {
			$this->field($field);
		}
		$info = $this->get([$pk => $id]);
		return $info;
	}
	/**
	 * 查询数据，返回列表
	 * @param  [type]  $where  [查询条件]
	 * @param  integer $limit  [查询数量]
	 * @param  integer $offset [起始查询数量]
	 * @param  boolean $field  [字段]
	 * @return [type]          [查询列表]
	 */
	public function list_data($where,$limit = 0,$offset = 0,$field=true){
		$pk = $this->getPk();
		$list = $this->where($where)->field($field)->limit($offset,$limit)->order($pk,"desc")->select();
        return $list;
	}
	protected function getAlldataAttr($value, $data){
		return $data;
	}
	public function list_datad($where,$page = 1,$count = 0,$field=true,$order=''){
		if (!$count) {
			$count = config('PAGE_COUNT');
		}
		if ($page == 0) {
			$page = 0;
			$count = 0;
		}
		if (!$order) {
			$pk = $this->getPk();
			$order = "$pk desc";
		}
		$list = $this->where($where)->field($field)->page($page,$count)->order($order)->select();
        return $list;
	}
	public function admin_get_list($offset = 0,$limit = 0,$where = [],$order='asc'){
		$pk = $this->getPk();
		$this->where($where);
		if ($limit) {
			$this->limit($offset,$limit);
		}
		$list = $this->order($pk,$order)->select();
		$count = $this->where($where)->count();
		$lists = [];
        foreach ($list as $v) {
        	$data = $v['alldata'];
        	$lists[] = $data;
        }
        return [$lists,$count];
	}
	public function get_list($searchData=[],$whereData=[],$limit = 0,$offset = 0,$order='asc'){
		$where = get_map($searchData,$whereData);
        $whereOr = get_or($searchData);
        $pk = $this->getPk();
        $list = $this->where($where)->whereOr($whereOr)->limit($offset,$limit)->order($pk,$order)->select();
        $lists = [];
        foreach ($list as $v) {
        	$data = $v['alldata'];
        	$lists[] = $data;
        }
        return [$lists,$where];
	}

	/**
     * 设置错误信息
     *
     * @param $error 错误信息
     * @return Auth
     */
    public function setError($error)
    {
        $this->_error = $error;
        return $this;
    }

    /**
     * 获取错误信息
     * @return string
     */
    public function getError()
    {
        return $this->_error ? $this->_error : '';
    }
}